/***************************************************************************\
 * Copyright (C) by Keio University
 * CVColorInputStream.cpp created in 10 2011.
 * Mail : fdesorbi@hvrl.ics.keio.ac.jp
 *
 * CVColorInputStream.cpp is part of the HVRL Engine Project.
 *
 * The HVRL Engine Project is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * The HVRL Engine Project is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
\***************************************************************************/

#include "hvrl/video/CVColorInputStream.hpp"

namespace hvrl{

CVColorInputStream::CVColorInputStream(const int& id, cv::Size size) throw(Exception)
{
	this->stream.open(id);

	if(this->stream.isOpened() == false)
	{
		throw Exception("CVColorInputStream::CVColorInputStream","Cannot initialize the OpenCV Camera");
	}

    	this->stream.set(CV_CAP_PROP_FRAME_WIDTH,size.width);
    	this->stream.set(CV_CAP_PROP_FRAME_HEIGHT,size.height);

	this->size.width = this->stream.get(CV_CAP_PROP_FRAME_WIDTH);
        this->size.height = this->stream.get(CV_CAP_PROP_FRAME_HEIGHT);

	this->stream >> frame;
}

CVColorInputStream::~CVColorInputStream(void)
{
    this->stream.release();

}

void CVColorInputStream::update(void)
{
    	this->stream >> this->frame;
}

double CVColorInputStream::getParameter(const int& id)
{
	return this->stream.get(id);
}

bool CVColorInputStream::setParameter(const int& id, const double& value)
{
	this->stream.set(id,value);
	if(id == CV_CAP_PROP_FRAME_WIDTH)
	this->size.width = this->stream.get(CV_CAP_PROP_FRAME_WIDTH);

	if(id == CV_CAP_PROP_FRAME_HEIGHT)
        this->size.height = this->stream.get(CV_CAP_PROP_FRAME_HEIGHT);
	return true;

}

}
